#include <iostream>
#include <vector>
#include <cassert>
#include <unistd.h>
#include <cstdlib>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "Task.hpp"

#define PROCESS_NUM 5

using namespace std;

int waitCommand(int waitFd, bool &quit)
{
    uint32_t command = 0;
    int n = read(waitFd, &command, sizeof(command));
    if (n == 0)
    {
        quit = true;
        return -1;
    }
    assert(n == sizeof(uint32_t));
    return command;
}

void sendAndWakeUp(pid_t who, int fd, uint32_t command)
{
    write(fd, &command, sizeof(command));
    cout << "call process: " << who << " execute " << desc[command] << " through " << fd << endl;
}

int main()
{
    load();
    vector<pair<pid_t, int>> slots;
    // 创建多个子进程
    for (int i = 0; i < PROCESS_NUM; i++)
    {
        // 创建管道
        int pipefd[2] = {0};
        int n = pipe(pipefd);
        assert(n == 0);
        (void)n;

        pid_t id = fork();
        assert(id != -1);
        // 子进程 读
        if (id == 0)
        {
            // 关闭写端
            close(pipefd[1]);
            while (true)
            {
                // 等命令
                bool quit = false;
                int command = waitCommand(pipefd[0], quit); // 如果对方不发则阻塞
                if (quit)
                    break;
                // 执行命令
                if (command >= 0 && command < handlerSize())
                {
                    callbacks[command]();
                }
                else
                {
                    cout << "非法command:" << command << endl;
                }
            }
            exit(1);
        }
        // 父进程 写
        // 关闭读端
        close(pipefd[0]);
        slots.push_back(make_pair(id, pipefd[1]));
    }
    // 父进程派发命令
    srand((unsigned long)time(nullptr) ^ getpid() ^ 2342344L);
    while (true)
    {
        // 选择命令
        uint32_t command = rand() % handlerSize();
        // 选择进程
        int proID = rand() % slots.size();
        // 派发任务
        sendAndWakeUp(slots[proID].first, slots[proID].second, command);
        sleep(1);

        // int select;
        // uint32_t command;
        // cout << "******************************************" << endl;
        // cout << "*  1.show functions      2.send command  *" << endl;
        // cout << "******************************************" << endl;
        // cout << "Please select > ";
        // cin >> select;
        // if (select == 1)
        //     showHandler();
        // else if (select == 2)
        // {
        //     cout << "Enter your command > ";
        //     // 选择命令
        //     cin >> command;
        //     // 选择进程
        //     int proID = rand() % slots.size();
        //     // 派发任务
        //     sendAndWakeUp(slots[proID].first, slots[proID].second, command);
        // }
        // else
        // {
        //     cout << "输入错误！" << endl;
        // }
    }

    // 关闭所有写端fd， 子进程退出
    for (const auto &slot : slots)
    {
        close(slot.second);
    }
    // 回收子进程
    for (const auto &slot : slots)
    {
        waitpid(slot.first, nullptr, 0);
    }

    return 0;
}